﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows;
using System.Timers;
using System.Reflection;

namespace TouchUmlUI
{
    public class TreeModelView : Canvas
    {
        private Window1 windowParent;
        public OurListBox listBox;
        private bool showing = false;
        private OurTabbedConrol otc;
        private Canvas toolBar;
        private HoverButton btnDock;
        private HoverButton btnClose;
        private bool docked = false;
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private double xacc = 0.0f;
        private double delta = 0.0f;        

        public void setTabbedControl(OurTabbedConrol otc)
        {
            this.otc = otc;
        }

        public bool isDocked()
        {
            return docked;
        }

        public void SetSelectedItem(TreeView control, object item)
        {
            try
            {
                DependencyObject dObject = control
                    .ItemContainerGenerator
                    .ContainerFromItem(item);

                //uncomment the following line if UI updates are unnecessary
                ((TreeViewItem)dObject).IsSelected = true;                

                MethodInfo selectMethod =
                   typeof(TreeViewItem).GetMethod("Select",
                   BindingFlags.NonPublic | BindingFlags.Instance);

                selectMethod.Invoke(dObject, new object[] { true });
            }
            catch { }
        }

        public void ZaznaczWModelu(Diagram.ElementNaDiagramie end,TreeViewItem root)
        {
            Console.WriteLine("----------");
            Console.WriteLine("root: " + root);
            if (root == null)
            {
                ZaznaczWModelu(end, listBox.Items[0] as TreeViewItem);
                ZaznaczWModelu(end, listBox.Items[1] as TreeViewItem);
            }
            else
            {
                for (int i = 0;i<root.Items.Count;i++) 
                {  
                    object ti = root.Items[i];
                    Console.WriteLine("ti: " + ti);
                    if (ti is TreeViewItem)
                    {
                        TreeViewItem tvi = ti as TreeViewItem;
                        if (tvi.Tag == null) continue;
                        if (tvi.Tag.Equals(end))
                        {
                            tvi.IsExpanded = true;
                            tvi.IsSelected = true;
                            tvi.Focus();
                            listBox.Focus();
                            ((TreeViewItem)tvi.Parent).IsExpanded = true;
                            foreach (object obj in tvi.Items)
                            {
                                if (obj is TreeViewItem)
                                {
                                    if (((TreeViewItem)obj).HasItems)
                                        ((TreeViewItem)obj).IsExpanded = true;
                                }
                            }
                            return;
                        }                        
                        ZaznaczWModelu(end, tvi);
                    }
                    else if (ti is ItemActor)
                    {
                        ItemActor ia = ti as ItemActor;                           
                        if (ia.myGuml.obj_uml.Equals(end))
                        {
                            ((TreeViewItem)listBox.Items.GetItemAt(1)).IsExpanded = true;
                            DependencyObject dObject = root.ItemContainerGenerator.ContainerFromItem(ia);                            
                            ((TreeViewItem)dObject).IsSelected = true;               
                        }
                    }
                    else if (ti is ItemAction)
                    {
                        ItemAction ia = ti as ItemAction;
                        if (ia.myGuml.obj_uml.Equals(end))
                        {
                            ((TreeViewItem)listBox.Items.GetItemAt(1)).IsExpanded = true;
                            DependencyObject dObject = root.ItemContainerGenerator.ContainerFromItem(ia);
                            ((TreeViewItem)dObject).IsSelected = true;
                        }
                    }
                }
            }
        }

        public void setHeight(double aheight)
        {
            listBox.Height = aheight - 20;
            this.Height = aheight;
        }
        public TreeModelView(Diagram.ModelDiagramu model, Window1 windowParent, double width,double height)
        {   
            timer.Enabled = false;
            timer.Interval = 1;
            timer.Tick += new EventHandler(timer_Tick);
            listBox = new OurListBox();
            listBox.model = model;
            this.windowParent = windowParent;            
            listBox.Width = this.Width = width;
            listBox.Height = this.Height = height;            
            this.MouseLeave += mouseLeave;            

            toolBar = new Canvas();
            toolBar.Width = width;
            toolBar.Height = 20;           

            LinearGradientBrush lgb = new LinearGradientBrush();
            lgb.StartPoint = new System.Windows.Point(0, 0);
            lgb.EndPoint = new System.Windows.Point(0, 1);
            lgb.GradientStops.Add(new GradientStop(Color.FromRgb(211,226,243),0.0f));
            lgb.GradientStops.Add(new GradientStop(Color.FromRgb(231,242,225),1.0f));
            toolBar.Background = lgb;

            btnDock = new HoverButton("ikony\\icon_toolbardock.png", "ikony\\icon_toolbardockhover.png", 17, 17);
            btnClose = new HoverButton("ikony\\icon_toolbarclose.png", "ikony\\icon_toolbarclosehover.png", 17, 17);
            btnDock.MouseDown += mouseDownDock;
            btnClose.MouseDown += mouseDownClose;
            toolBar.Children.Add(btnClose);            
            toolBar.Children.Add(btnDock);
            Canvas.SetTop(btnClose, 1);
            Canvas.SetTop(btnDock, 1);
            Canvas.SetLeft(btnClose, 20);
            Canvas.SetLeft(btnDock, 1);
            Canvas.SetTop(listBox, 20);

            Children.Add(toolBar);
            Children.Add(listBox);                        
            listBox.VerticalAlignment = VerticalAlignment.Stretch;            
            this.Background = Brushes.Red;
            Canvas.SetZIndex(this, 105);            
            Canvas.SetLeft(this,windowParent.ActualWidth);
            delta = 0.0f;
            this.Visibility = System.Windows.Visibility.Hidden;            
        }

        void timer_Tick(object sender, EventArgs e)
        {            
            if (delta >= this.ActualWidth + 15)
            {                
                delta = this.ActualWidth + 15;                
                showing = false;                
                timer.Enabled = false;
                if (xacc > 0)
                    this.Visibility = Visibility.Hidden;
                else
                    Canvas.SetLeft(this, windowParent.ActualWidth - delta);
                delta = 0.0f;
                return;
            }

            if (xacc < 0)
            {
                Canvas.SetLeft(this, windowParent.ActualWidth - delta);
                if (delta > 10)
                    this.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                Canvas.SetLeft(this, windowParent.ActualWidth - this.ActualWidth - 15 + delta);
            }

            delta += 6.5f * Math.Abs(xacc);

        }
 
        private void mouseDownDock(object sender, MouseEventArgs e)
        {
            docked = true;
            windowParent.Window1_SizeChanged(sender, null);
        }

        private void mouseDownClose(object sender, MouseEventArgs e)
        {
            docked = false;
            windowParent.Window1_SizeChanged(sender, null);            
        }

        public void Refresh()
        {
            Console.WriteLine("TreeModelView.Refresh()");
            Console.WriteLine("OTC: " + otc);
            Console.WriteLine("MODEL: " + listBox.model);
            listBox.Refresh(otc);
        }

        public bool isShowing()
        {
            return showing;
        }

        private void mouseLeave(object sender, MouseEventArgs e)
        {
            if (!docked)
            {
                if (this.isVisible())
                {                   
                        Show(false);
                }
            }
        }

        public bool isVisible()
        {
            return this.Visibility == System.Windows.Visibility.Hidden ? false : true;
        }

        public void Show(bool a_show)
        {

            if (showing) {
                if (timer.Enabled)
                {
                    if ((xacc < 0) && (!a_show))
                    {
                        delta = this.ActualWidth + 15 - delta;
                    }
                    else
                        return;
                }
                else
                    if (a_show)
                    {
                        Canvas.SetLeft(this, 4000);
                    }
            }

            if (a_show) 
            {
                                               
                timer.Enabled = true;                
                xacc = -1.0f;
                showing = true;
            }
            else
            {
                timer.Enabled = true;                
                xacc = 1.0f;
                showing = true;
            }
        }

    }
}
